package com.hantianle.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ajaxServlet")
public class AjaxServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、获取请求参数
        String username = request.getParameter("username");

        //处理业务逻辑。（一旦处理，就会耗时间。）
        try {
            Thread.sleep(5000); //“睡5秒”
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //2、打印username
        System.out.println(username);
        //3、响应
        response.getWriter().write("hello "+username+" ~");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

